#include "adc.h"
uint16_t adc_measurements_[ADC_CHANNEL_COUNT];

void ADC0_Init(void){

    /* ADC_DMA_channel configuration */
    dma_single_data_parameter_struct dma_single_data_parameter;
    rcu_periph_clock_enable(RCU_DMA1);
    /* ADC DMA_channel configuration */
    dma_deinit(DMA1, DMA_CH0);
    /* initialize DMA single data mode */
    dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
    dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_single_data_parameter.memory0_addr = (uint32_t)(adc_measurements_);
    dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
    dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
    dma_single_data_parameter.number = ADC_CHANNEL_COUNT;
    dma_single_data_parameter.circular_mode = ENABLE;
    dma_single_data_parameter.priority = DMA_PRIORITY_LOW;
    dma_single_data_mode_init(DMA1, DMA_CH0, &dma_single_data_parameter);
    dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI0);
    /* enable DMA channel */
    dma_channel_enable(DMA1, DMA_CH0);
    //nvic_irq_enable(DMA1_Channel0_IRQn,4,0);
    //dma_interrupt_enable(DMA1,DMA_CH0,DMA_CHXCTL_FTFIE);

    //使能ADC0时钟
    rcu_periph_clock_enable(RCU_ADC0);
    adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
    //右对齐
    adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
    //扫描模式
    adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);
    adc_special_function_config(ADC0,ADC_CONTINUOUS_MODE,ENABLE);
    adc_discontinuous_mode_config(ADC0,ADC_CHANNEL_DISCON_DISABLE,0);
    //12B 4096
    adc_resolution_config(ADC0,ADC_RESOLUTION_12B);
    //规则通道长度
    adc_channel_length_config(ADC0,ADC_REGULAR_CHANNEL,ADC_CHANNEL_COUNT);
    //规则通道外部触发关闭
    adc_external_trigger_config(ADC0,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
    //软件触发使能
    for (uint32_t channel = 0; channel < ADC_CHANNEL_COUNT; ++channel) {
        adc_regular_channel_config(ADC0,channel,channel,ADC_SAMPLETIME_15);
    }
    //注入通道
    adc_channel_length_config(ADC0,ADC_INSERTED_CHANNEL,1);//注入通道长度1
    adc_external_trigger_config(ADC0,ADC_INSERTED_CHANNEL,EXTERNAL_TRIGGER_RISING); //外部上升沿中断
    adc_external_trigger_source_config(ADC0,ADC_INSERTED_CHANNEL,ADC_EXTTRIG_INSERTED_T0_TRGO); //高级定时器0更新中断
    adc_inserted_channel_config(ADC0,0,ADC_CHANNEL_6,ADC_SAMPLETIME_3);                         //通道6
    adc_special_function_config(ADC0,ADC_INSERTED_CHANNEL_AUTO,DISABLE);               //自动转换模式关闭
    //循环DMA模式
    adc_dma_request_after_last_enable(ADC0);
    adc_dma_mode_enable(ADC0);
    adc_end_of_conversion_config(ADC0,ADC_EOC_SET_SEQUENCE);
    nvic_irq_enable(ADC_IRQn,5,0);
    //adc_enable(ADC0);
    //adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL); 
}
void ADC1_Init(void){
    /* enable ADC1 clock */
    rcu_periph_clock_enable(RCU_ADC1);
    /* config ADC clock */
    adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
    /* configure the ADC sync mode */
    adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC1,ADC_DATAALIGN_RIGHT);
    /* ADC SCAN function disable */
    adc_special_function_config(ADC1,ADC_SCAN_MODE,DISABLE);
    adc_special_function_config(ADC1,ADC_CONTINUOUS_MODE,DISABLE);
    adc_discontinuous_mode_config(ADC1,ADC_CHANNEL_DISCON_DISABLE,0);
    /* configure ADC resolution */
    adc_resolution_config(ADC1,ADC_RESOLUTION_12B);
    /* ADC channel length config */
    adc_channel_length_config(ADC1,ADC_REGULAR_CHANNEL,1);
    adc_channel_length_config(ADC1,ADC_INSERTED_CHANNEL,1);
    /* ADC regular channel config */
    adc_regular_channel_config(ADC1,0,ADC_CHANNEL_13,ADC_SAMPLETIME_3);
    adc_external_trigger_config(ADC1,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_RISING);
    adc_external_trigger_source_config(ADC1,ADC_REGULAR_CHANNEL,ADC_EXTTRIG_REGULAR_T7_TRGO);
    /* configure end of conversion mode */
    adc_end_of_conversion_config(ADC1,ADC_EOC_SET_SEQUENCE);
    /* ADC insert channel config */
    adc_special_function_config(ADC1,ADC_INSERTED_CHANNEL_AUTO,DISABLE);               //自动转换模式关闭
    adc_inserted_channel_config(ADC1,0,ADC_CHANNEL_10,ADC_SAMPLETIME_3);
    adc_external_trigger_config(ADC1,ADC_INSERTED_CHANNEL,EXTERNAL_TRIGGER_RISING);
    adc_external_trigger_source_config(ADC1,ADC_INSERTED_CHANNEL,ADC_EXTTRIG_INSERTED_T0_TRGO);
    
    /* clear the ADC flag */
    adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOIC);
    /* enable ADC interrupt */
    adc_interrupt_enable(ADC1, ADC_INT_EOC);
    adc_interrupt_enable(ADC1, ADC_INT_EOIC);

    adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOIC);
    nvic_irq_enable(ADC_IRQn,5,0);
}
void ADC2_Init(void){
    /* enable ADC2 clock */
    rcu_periph_clock_enable(RCU_ADC2);
    /* config ADC clock */
    adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
    /* configure the ADC sync mode */
    adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC2,ADC_DATAALIGN_RIGHT);
    /* ADC SCAN function disable */
    adc_special_function_config(ADC2,ADC_SCAN_MODE,DISABLE);
    adc_special_function_config(ADC2,ADC_CONTINUOUS_MODE,DISABLE);
    adc_discontinuous_mode_config(ADC2,ADC_CHANNEL_DISCON_DISABLE,0);
    /* configure ADC resolution */
    adc_resolution_config(ADC2,ADC_RESOLUTION_12B);
    /* ADC channel length config */
    adc_channel_length_config(ADC2,ADC_REGULAR_CHANNEL,1);
    adc_channel_length_config(ADC2,ADC_INSERTED_CHANNEL,1);
    /* ADC regular channel config */
    adc_regular_channel_config(ADC2,0,ADC_CHANNEL_12,ADC_SAMPLETIME_3);
    adc_external_trigger_config(ADC2,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_RISING);
    adc_external_trigger_source_config(ADC2,ADC_REGULAR_CHANNEL,ADC_EXTTRIG_REGULAR_T7_TRGO);
    /* configure end of conversion mode */
    adc_end_of_conversion_config(ADC2,ADC_EOC_SET_SEQUENCE);
    /* ADC insert channel config */
    adc_special_function_config(ADC2,ADC_INSERTED_CHANNEL_AUTO,DISABLE);               //自动转换模式关闭
    adc_inserted_channel_config(ADC2,0,ADC_CHANNEL_11,ADC_SAMPLETIME_3);
    adc_external_trigger_config(ADC2,ADC_INSERTED_CHANNEL,EXTERNAL_TRIGGER_RISING);
    adc_external_trigger_source_config(ADC2,ADC_INSERTED_CHANNEL,ADC_EXTTRIG_INSERTED_T0_TRGO);
    
    /* clear the ADC flag */
    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOIC);
    /* enable ADC interrupt */
    adc_interrupt_enable(ADC2, ADC_INT_EOC);
    adc_interrupt_enable(ADC2, ADC_INT_EOIC);

    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC2, ADC_INT_FLAG_EOIC);
    nvic_irq_enable(ADC_IRQn,5,0);
}